package org.common.plugin;

import java.net.URL;
import java.net.URLClassLoader;
import javax.annotation.Resource;
import org.common.service.DependenceManager;

/**
 *
 * @author gus
 */
public class PluginManager
{
    @Resource(name="pluginRegistry")
    private PluginRegistry pluginRegistry;

    @Resource(name="dependenceManager")
    private DependenceManager dependenceManager;

    public PluginInfo  getPluginInfo(String name) throws Exception
    {
        return pluginRegistry.getPluginInfo(name);
    }

    public PluginInfo [] getPluginInfos() throws Exception
    {
        return pluginRegistry.getPluginInfos();
    }

    public <T> Plugin<T> loadPlugin(PluginInfo info) throws Exception
    {
        ClassLoader loader = new URLClassLoader(new URL[]{info.getFilePath().toURI().toURL()}, getClass().getClassLoader());

        Class<Plugin<T>> clazz = (Class<Plugin<T>>) loader.loadClass(info.getClassName());
        
        Plugin<T> plugin = clazz.newInstance();
        plugin.setDependencyManager(dependenceManager);
        plugin.setPluginInfo(info);
        
        return plugin;
    }
    
    public void unloadPlugin(PluginInfo info)
    {
    }
}
